package com.cwj.juc;

import java.util.concurrent.*;

/**
 * @author Chenwujie
 * 2021/5/24 21:13
 * 生产者消费者问题：一个工人每小时生产10个零件，工厂每半小时消耗6个零件，当零件缺少10个以上时增加工人数量，当零件超过10个以上时缩减工人数量。
 */

public class PCTest {
    public static void main(String[] args) throws InterruptedException {
        BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<>(20);
        ExecutorService pool = Executors.newFixedThreadPool(2);

        for(;;){
            TimeUnit.SECONDS.sleep(5);
            blockingDeque.poll();
            System.out.println("总量：" + blockingDeque.size());
        }
    }

    public static class MyTask implements Runnable{
        private BlockingDeque<Integer> blockingDeque;
        public MyTask(BlockingDeque<Integer> blockingDeque) {
            this.blockingDeque = blockingDeque;
        }

        @Override
        public void run() {
            for(;;){
                try {
                    TimeUnit.SECONDS.sleep(6);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                blockingDeque.offer(1);
            }
        }
    }

}
